home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_068 / mg1b / tty / amiga / ttykbd.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  12KB  |  433 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *         Amiga virtual terminal keyboard, default console keymap.
  4.  * Version:    Gnu v30
  5.  * Last edit:    25-Oct-86
  6.  * Created:    19-Apr-86 ...!ihnp4!seismo!ut-sally!ut-ngp!mic
  7.  *        This goes with the Intuition terminal driver,
  8.  *        and implements Mike Meyer's hot mouse.
  9.  */
  10. #include    <exec/types.h>
  11. #include    <intuition/intuition.h>
  12. #undef    TRUE
  13. #undef    FALSE
  14. #include    "def.h"
  15.  
  16. #define    ESC    0x1B            /* Escape, arrows et al.    */
  17. #define    CSI    0x9B            /* Amiga CSI            */
  18.  
  19. #ifdef    MOUSE
  20. /* Stuff for the hot mouse.  Since right now the mouse
  21.  * keys get bound into a wierd place, a lot of the
  22.  * jiggery-pokery up here is to make it possible
  23.  * to shift their location in the keymap without too
  24.  * much fuss.  Sorry for the horrid macro names...
  25.  */
  26.  
  27. /* key code w/o mini qualifiers */
  28. #define    WMOUSE    ((KEY)(KCTRL | KMETA | 'a'))
  29. #define SMOUSE    ((KEY)(KCTRL | KMETA | 'i'))
  30. #define    EMOUSE    ((KEY)(KCTRL | KMETA | 'q'))
  31.  
  32. /* mini qualifiers */
  33. #define    SHFT    1
  34. #define ALT    2
  35. #define CTRL    4
  36.  
  37. /* macros to create qualified key codes */
  38.  
  39. #define    S(k)    (k + SHFT)
  40. #define    A(k)    (k + ALT)
  41. #define    C(k)    (k + CTRL)
  42. #define    AS(k)    (k + SHFT + ALT)
  43. #define    CS(k)    (k + CTRL + SHFT)
  44. #define    CA(k)    (k + CTRL + ALT)
  45. #define    CAS(k)    (k + SHFT + ALT + CTRL)
  46. #endif /* ifdef MOUSE */
  47.  
  48. #ifdef    XKEYS
  49. /*
  50.  * The function keys on the Amiga send back
  51.  * escape sequences of the form <ESC>[code~, where code
  52.  * is a one or two-character code for the key.  To make
  53.  * it easier to decode, we place the internal key values
  54.  * for these codes in this table.
  55.  */
  56.  
  57. short    consolemap[] = {
  58.     KF1,        KF2,        KF3,        KF4,
  59.     KF5,        KF6,        KF7,        KF8,
  60.     KF9,        KF10,        KSF1,        KSF2,
  61.     KSF3,        KSF4,        KSF5,        KSF6,
  62.     KSF7,        KSF8,        KSF9,        KSF10
  63. };
  64. #define    NFUNCKEYS ((sizeof consolemap)/(sizeof consolemap[0]))
  65. #endif
  66.  
  67. /*
  68.  * Names for the keys with basic keycode
  69.  * between KFIRST and KLAST (inclusive). This is used by
  70.  * the key name routine in "kbd.c".  KFIRST is KRANDOM,
  71.  * which we don't bind anything useful to.  "The Menu" is
  72.  * special; we implement menus as another function key,
  73.  * but there isn't really any "MENU" key on the keyboard.
  74.  * There is no shifted value for the help key.  Mouse clicks
  75.  * have been moved to a hitherto unused part of the keymap.
  76.  */
  77. #ifdef    DO_MENU
  78. #define MENUNAME "The Menu"
  79. #else
  80. #define MENUNAME NULL
  81. #endif
  82.  
  83. char    *keystrings[] = {
  84. #ifdef    XKEYS
  85.     NULL,        "F1",        "F2",        "F3",
  86.     "F4",        "F5",        "F6",        "F7",
  87.     "F8",        "F9",        "F10",        "Shift-F1",
  88.     "Shift-F2",    "Shift-F3",    "Shift-F4",    "Shift-F5",
  89.     "Shift-F6",    "Shift-F7",    "Shift-F8",    "Shift-F9",
  90.     "Shift-F10",    "Up",        "Shift-Up",    "Down",
  91.     "Shift-Down",    "Left",        "Shift-Left",    "Right",
  92.     "Shift-Right",    "Help",        MENUNAME,    NULL
  93. #else
  94.     NULL,        NULL,        NULL,        NULL,
  95.     NULL,        NULL,        NULL,        NULL,
  96.     NULL,        NULL,        NULL,        NULL,
  97.     NULL,        NULL,        NULL,        NULL,
  98.     NULL,        NULL,        NULL,        NULL,
  99.     NULL,        NULL,        NULL,        NULL,
  100.     NULL,        NULL,        NULL,        NULL,
  101.     NULL,        NULL,        MENUNAME,    NULL
  102. #endif
  103. };
  104.  
  105. /*
  106.  * Read in a key, doing the low level mapping
  107.  * of ASCII code to 11 bit code. This level deals with
  108.  * mapping the special keys into their spots in the C1
  109.  * control area. The C0 controls go right through, and
  110.  * get remapped by "getkey".  Returning function keys
  111.  * with KMETA set distinguishes these keys from codes
  112.  * generated by ALT-ing a control key (which I want
  113.  * to have mapped in the usual way).
  114.  */
  115.  
  116. getkbd()
  117. {
  118.     register int    c;
  119. #ifdef    XKEYS
  120.     register int    n;
  121. #endif
  122. loop:
  123.     c = ttgetc();
  124.     if (c == CSI) {
  125.         c = ttgetc();
  126. #ifdef    MOUSE
  127.         if (c == 'P') {            /* mouse sequence    */
  128.             ttgetc();        /* discard '~'        */
  129.             return (KCTRL | getmouse());    /* create key code    */
  130.         }
  131. #endif
  132.  
  133. #ifdef    DO_MENU
  134.         if (c == 'M') {            /* (fake) menu key    */
  135.             ttgetc();        /* discard '~'        */
  136.             return (KCTRL | KMENU);
  137.         }
  138. #endif
  139.  
  140. #ifdef    XKEYS
  141.         if (c == '?') {            /* HELP key        */
  142.             ttgetc();        /* discard '~'        */
  143.             return (KCTRL | KHELP);
  144.         }
  145.         /* Arrow keys */
  146.         if (c == 'A')
  147.             return (KCTRL | KUP);
  148.         if (c == 'B')
  149.             return (KCTRL | KDOWN);
  150.         if (c == 'C')
  151.             return (KCTRL | KRIGHT);
  152.         if (c == 'D')
  153.             return (KCTRL | KLEFT);
  154.         if (c == 'T')
  155.             return (KCTRL | KSUP);
  156.         if (c == 'S')
  157.             return (KCTRL | KSDOWN);
  158.         /* Shifted left, right arrow */
  159.         if (c == ' ') {
  160.             c = ttgetc();
  161.             if (c == 'A' || c == '@')
  162.                 return (KCTRL | ((c == 'A') ?
  163.                     (KSLEFT) : (KSRIGHT)));
  164.             goto loop;        /* try again, sucker */
  165.         }
  166.  
  167.         /* Function keys    */
  168.         if (c >= '0' && c <= '9') {
  169.             n = 0;
  170.             do {
  171.                 n = 10*n + c - '0';
  172.                 c = ttgetc();
  173.             } while (c>='0' && c<='9');
  174.             if (c == '~' && n < NFUNCKEYS) {
  175.                 c = consolemap[n];
  176.                 if (c != KRANDOM)
  177.                     return (KCTRL | c);
  178.                 goto loop;
  179.             }
  180.             else 
  181.                 goto loop;    /* Try again */
  182.         }
  183. #endif
  184.         goto loop;        /* Try again */
  185.     }
  186.     return (c);
  187. }
  188.  
  189. #ifdef    MOUSE
  190. /*
  191.  * A hack for the hot mouse -- peek ahead at the
  192.  * next mouse event and construct an internal key
  193.  * code.  Might as well use those extra binding slots...
  194.  */
  195.  
  196. getmouse()
  197. {
  198.     USHORT        row, col, qual;
  199.     int ttmouse();
  200.     register int    code = 0;
  201.     register struct WINDOW *wp;
  202.  
  203.     ttmouse(FALSE, &row, &col, &qual);    /* look at mouse */
  204.  
  205.     /* was the click in a window ???    */
  206.     for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
  207.         if ((row >= wp->w_toprow) &&
  208.             (row <= (wp->w_toprow + wp->w_ntrows)))
  209.             break ;
  210.  
  211.     /* figure out what area the click was in            */
  212.     if (wp == NULL)            /* not found; assume echo line    */
  213.         code = EMOUSE;
  214.     else if (row == (wp->w_toprow + wp->w_ntrows))    /* status line    */
  215.         code = SMOUSE;
  216.     else
  217.         code = WMOUSE;    /* click in a window        */
  218.  
  219.     /* figure out 'mini' qualifiers -- ADD them, because 'a' isn't    */
  220.     /* divisible by 2...  This took me 2 hours to realize...           */
  221.     if (qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  222.         code += SHFT;
  223.     if (qual & (IEQUALIFIER_LALT | IEQUALIFIER_RALT))
  224.         code += ALT;
  225.     if (qual & IEQUALIFIER_CONTROL)
  226.         code += CTRL;
  227.  
  228.     return (code);
  229. }
  230. #endif
  231.  
  232. /*
  233.  * Terminal specific keymap initialization.
  234.  *
  235.  * Bind all of the assigned graphics in the
  236.  * Amiga alternate character set to self-insert.
  237.  *
  238.  * #ifdef XKEYS, attach the special keys to the appropriate
  239.  * built-in functions.
  240.  *
  241.  * #ifdef DO_MENU, bind the fake KMENU code to amigamenu()
  242.  * to do menu selection as transparently as possible.
  243.  *
  244.  * #ifdef MOUSE, bind the hot mouse keys to the
  245.  * functions. In the case of window and mode line
  246.  * selections, call special functions
  247.  * special functions that select the appropriate
  248.  * object (i.e. character, window) before performing
  249.  * the task.
  250.  *
  251.  * As is the case of all the keymap routines, errors
  252.  * result in panic.
  253.  */
  254.  
  255. extern    int    togglewindow();        /* Defined by "ttyio.c"        */
  256.  
  257. #ifdef    DO_MENU
  258. extern    int    amigamenu();        /* Defined by "ttymenu.c"     */
  259. #endif
  260.  
  261. #ifdef    CHANGE_FONT
  262. extern    int    setfont();        /* Defined by "ttyio.c"        */
  263. #endif
  264.  
  265. #ifdef    MOUSE
  266. extern    int    amigamouse();        /* Defined by "ttymouse.c"    */
  267. extern    int    mreposition();        /* Functions which select the    */ 
  268. extern    int    mdelfword();        /* window the click was in,    */
  269. extern    int    mkillline();        /* then call another one    */
  270. extern    int    mforwdel();        /* Defined in "ttymouse.c"    */
  271. extern    int    mdelwhite();
  272. extern    int    mkillregion();
  273. extern    int    myank();
  274. extern    int    mforwpage();
  275. extern    int    mbackpage();
  276. extern    int    msplitwind();
  277. extern    int    mdelwind();
  278. extern    int    mgotobob();
  279. extern    int    mgotoeob();
  280. extern    int    menlargewind();
  281. extern    int    mshrinkwind();
  282. #endif
  283.  
  284. #ifdef    CHANGE_COLOR
  285.     /* functions to mess with the mode line rendition, window colors*/
  286. extern    int    ttmode();        /* Defined by "tty.c"        */
  287. extern    int    tttext();        /*  ""                */
  288. extern    int    textforeground();    /*  ""                */
  289. extern    int    textbackground();    /*  ""                */
  290. extern    int    modeforeground();    /*  ""                */
  291. extern    int    modebackground();    /*  ""                */
  292. #endif
  293.  
  294. VOID
  295. ttykeymapinit()
  296. {
  297.     register SYMBOL    *sp;
  298.     register int    i;
  299.  
  300.     /* Intuition window manipulation     */
  301.  
  302. #ifndef    MEYN
  303.     keydup((KEY)KMETA|KCTRL|'L',    "redraw-display");
  304. #endif
  305.      keyadd((KEY)-1,    togglewindow,    "toggle-window-hack");
  306.  
  307.     /*
  308.      * Bind all positions that correspond
  309.      * to characters in the Amiga alternate
  310.      * character set to "ins-self". These characters may
  311.      * be used just like any other character.  Of course,
  312.      * if DO_METAKEY is defined in kbd.c, the alternate character
  313.      * set will get mapped to META-ed keys...
  314.      */
  315.  
  316.     if ((sp=symlookup("self-insert-command")) == NULL)
  317.         panic("ttykeymapinit: can't find self-insert-command");
  318.     for (i=0xA0; i<0xFF; ++i) {
  319.         if (binding[i] != NULL)
  320.             panic("ttykeymapinit: key already bound");
  321.         binding[i] = sp;
  322.     }
  323.  
  324. #ifdef    DO_MENU
  325.     /* "Menu" key, if compiled in    */
  326.     keyadd((KEY)KMENU,    amigamenu,    "amiga-menu");
  327. #endif
  328.  
  329. #ifdef    CHANGE_FONT
  330.     keyadd((KEY)-1,    setfont,    "set-font");
  331. #endif
  332.  
  333. #ifdef    CHANGE_COLOR
  334.     /* Functions to allow you to change colors    */
  335.     keyadd((KEY)-1, ttmode,        "set-mode-rendition");
  336.     keyadd((KEY)-1,    tttext,        "set-text-rendition");
  337.     keyadd((KEY)-1,    textforeground,    "set-text-foreground");
  338.     keyadd((KEY)-1,    textbackground,    "set-text-background");
  339.     keyadd((KEY)-1, modeforeground, "set-mode-foreground");
  340.     keyadd((KEY)-1,    modebackground,    "set-mode-background");
  341. #endif
  342.  
  343. #ifdef    XKEYS
  344.     /* Arrow keys    */
  345.     keydup((KEY)KUP,    "previous-line");
  346.     keydup((KEY)KDOWN,    "next-line");
  347.  
  348.     keydup((KEY)KSUP,    "backward-paragraph");
  349.     keydup((KEY)KSDOWN,    "forward-paragraph");    
  350.  
  351.     keydup((KEY)KRIGHT,    "forward-char");
  352.     keydup((KEY)KLEFT,    "backward-char");
  353.  
  354.     keydup((KEY)KSRIGHT,    "forward-word");
  355.     keydup((KEY)KSLEFT,    "backward-word");
  356.  
  357.     /* Function keys     */
  358.     keydup((KEY)KHELP,    "describe-key-briefly");
  359.  
  360.     keydup((KEY)KF1,    "find-file");
  361.     keydup((KEY)KSF1,    "find-file-other-window");
  362.  
  363.     keydup((KEY)KF2,    "save-buffer");
  364.     keydup((KEY)KSF2,    "write-file");
  365.  
  366.     keydup((KEY)KF3,    "scroll-up");
  367.     keydup((KEY)KSF3,    "scroll-down");
  368.  
  369.     keydup((KEY)KF4,    "next-window");
  370.     keydup((KEY)KSF4,    "previous-window");
  371.  
  372.     keydup((KEY)KF5,    "enlarge-window");
  373.     keydup((KEY)KSF5,    "shrink-window");
  374.  
  375.     keydup((KEY)KF6,    "fill-paragraph");
  376.     keydup((KEY)KSF6,    "query-replace");
  377.  
  378.     keydup((KEY)KF7,    "split-window-vertically");
  379.     keydup((KEY)KSF7,    "delete-other-windows");
  380.  
  381.     keydup((KEY)KF8,    "global-set-key");
  382.     keydup((KEY)KSF8,    "global-unset-key");
  383.  
  384.     keydup((KEY)KF9,    "start-kbd-macro");
  385.     keydup((KEY)KSF9,    "end-kbd-macro");
  386.  
  387.     keydup((KEY)KF10,    "call-last-kbd-macro");
  388.     keydup((KEY)KSF10,    "save-buffers-kill-emacs");
  389. #endif
  390.  
  391. #ifdef    MOUSE
  392.     /* Mouse clicks in a window do editing functions on the    */
  393.     /* window.                        */
  394.  
  395.     keyadd(WMOUSE,        amigamouse,    "amiga-mouse");
  396.     keyadd(S(WMOUSE),    mreposition,    "mouse-recenter");
  397.     keyadd(A(WMOUSE),    mdelfword,    "mouse-kill-word");
  398.     keyadd(AS(WMOUSE),    mkillline,    "mouse-kill-line");
  399.     keyadd(C(WMOUSE),    mforwdel,    "mouse-delete-char");
  400.     keyadd(CS(WMOUSE),    mdelwhite,    "mouse-just-one-space");
  401.     keyadd(CA(WMOUSE),    mkillregion,    "mouse-kill-region");
  402.     keyadd(CAS(WMOUSE),    myank,        "mouse-yank");
  403.  
  404.     /* Mouse clicks in the status line select that window and    */
  405.     /* then perform a command on that window or buffer.        */
  406.     /* Use keyadd() because they haven't been bound before        */
  407.  
  408.     keyadd(SMOUSE,       mforwpage,    "mouse-scroll-up");
  409.     keyadd(S(SMOUSE),  mbackpage,    "mouse-scroll-down");
  410.     keyadd(A(SMOUSE),  msplitwind,    "mouse-split-window-vertically");
  411.     keyadd(AS(SMOUSE), mdelwind,    "mouse-delete-window");
  412.     keyadd(C(SMOUSE),  mgotobob,    "mouse-beginning-of-buffer");
  413.     keyadd(CS(SMOUSE), mgotoeob,    "mouse-end-of-buffer");
  414.     keyadd(CA(SMOUSE), menlargewind,"mouse-enlarge-window");
  415.     keyadd(CAS(SMOUSE),mshrinkwind,    "mouse-shrink-window");
  416.  
  417.     /* mouse clicks in echo line do global things        */
  418.  
  419. #ifdef    MEYN
  420.     keydup(EMOUSE,        "save-buffer");
  421. #else
  422.     keydup(EMOUSE,        "switch-to-buffer");
  423. #endif
  424.     keydup(S(EMOUSE),    "kill-buffer");
  425.     keydup(A(EMOUSE),    "describe-key-briefly");
  426.     keydup(AS(EMOUSE),    "describe-bindings");
  427.     keydup(C(EMOUSE),    "suspend-emacs");
  428.     keydup(CS(EMOUSE),    "save-buffers-kill-emacs");
  429.     keydup(CA(EMOUSE),    "list-buffers");
  430.     keydup(CAS(EMOUSE),    "toggle-window-hack");
  431. #endif
  432. }
  433.